package com.spl.mcm.views;

import java.util.ArrayList;
import java.util.HashMap;

import com.spl.mcm.R;
import com.spl.mcm.R.id;
import com.spl.mcm.R.layout;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ListItemAdapter extends BaseAdapter {

	private Activity activity;
    private ArrayList<ListItem> data;
    private static LayoutInflater inflater=null;
	private Boolean isEditting = false;
    Boolean[] itemsCheckedStatus;
    
    /**
	 * @return the isEditting
	 */
	public Boolean getIsEditting() {
		return isEditting;
	}

	/**
	 * @param isEditting the isEditting to set
	 */
	public void setIsEditting(Boolean isEditting) {
		this.isEditting = isEditting;
	}

	public ListItemAdapter(Activity a, ArrayList<ListItem> d){
    	activity = a;
    	this.data = d;
    	itemsCheckedStatus = new Boolean[d.size()];
    	setCheckForAll(false);
    	inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.intercept_list_item, null);
 
        // Overide by intercept list or history list
        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView subTitle = (TextView)vi.findViewById(R.id.subtitle); // name or number
        TextView additionInfo = (TextView)vi.findViewById(R.id.type); // intercept type
        //ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
 
        // disable edit button
        CheckBox checkBox = (CheckBox)vi.findViewById(R.id.editCheckBox);
        checkBox.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				itemsCheckedStatus[position] = !itemsCheckedStatus[position];
			}
		});
        if(!isEditting)
        	checkBox.setVisibility(View.INVISIBLE);
        else {
        	checkBox.setVisibility(View.VISIBLE);
        	checkBox.setChecked(itemsCheckedStatus[position]);
        	//Log.i("SUMPRO", "getView itemsCheckedStatus " + position + " - " + itemsCheckedStatus[position]);
        }
        checkBox.setTag(Integer.valueOf(position));
        
        
        
        ListItem item = data.get(position);
 
        // Setting all values in listview
        title.setText(item.getTitle());
        subTitle.setText(item.getSubTitle());
        additionInfo.setText(item.getAdditionInfor());
        return vi;
	}
	
	public void setCheckForAll(Boolean isChecked) {
		Log.i("SUMPRO", "setCheckForAll " + isChecked);
		for(int i = 0; i < itemsCheckedStatus.length; i++) {
			itemsCheckedStatus[i] = isChecked;
		}
	}

}
